import javax.script.ScriptException;
import javax.swing.JFrame;

import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusLocator;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.code.DataType;
import com.serotonin.modbus4j.code.RegisterRange;
import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.ip.IpParameters;




public class TestAppMainPage extends JFrame implements Runnable{

	/**
	 * @param args
	 */
	static Thread mythread;
	public static TestAppPage1 testup;
	
	//public static TestApp ADAMtes;
	static  float portValue;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//System.out.println("Running");
		testup = new TestAppPage1();
		testup.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		testup.setVisible(true);
		mythread = new Thread(new TestAppMainPage());
		mythread.start();
		//System.out.println("Rest of the pgm");
		
		
		
		
		
		
		
	}
	@Override
	public void run() {
		Communication c = new Communication();
		c.InitModbus();
		
		// TODO Auto-generated method stub
		boolean onetimeflag = true;
	//	System.out.println("Running" );
	//	System.out.println(testup.getPollingIntervel());
		//System.out.println("Running");
		long startTime = 0;
		long correctingTime = 0;
		
		while(true){
			try {
				Thread.sleep((testup.getPollingIntervel()));
				
			} catch (NumberFormatException | InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			startTime = System.currentTimeMillis();
			if(testup.getStartButtonStatus()){
				if(onetimeflag)
					portValue = testup.getStartingValue();
				onetimeflag = false;
				portValue = portValue + (testup.getRateOfChange()*655);
				c.WriteData((int)portValue, 3);
			}
			if(testup.getStopButtonStatus()){
				//System.out.println("Stop Button : On");
			}
			//System.out.println("Time took in Millisecond: " + correctingTime);	
		}
	}
	
	
	
	 
	
}
